package com.kaguya.factory;

/**
 * 工厂模式，工厂方法模式，定义抽象工厂Factories，由实现类工厂InvokeFactory1 InvokeFactory2来决定具体的实现。
 */
public class FactoryMethods {
    public static void main(String[] args) {
        Factories factories = new InvokeFactory1();
        factories.doSomething();

        Factories factories1 = new InvokeFactory2();
        factories1.doSomething();
    }
}

/**
 * 抽象类工厂，定义方法让它的实现类去做不同的实现，从而达到根据需求创建不同的对象的功能
 */
abstract class Factories {
    abstract public Product factoryMethod();

    public void doSomething() {
        Product product = factoryMethod();
        product.exec();
    }
}

/**
 * 具体实现接口
 */
interface Product {
    void exec();
}

/**
 * 具体不同的实现
 */
class Product1 implements Product {

    @Override
    public void exec() {
        System.out.println("exec i am product1 _created by InvokeFactory1");
    }
}

class Product2 implements Product {

    @Override
    public void exec() {
        System.out.println("exec i am product2 _created by InvokeFactory2");
    }
}

/**
 * 抽象工厂的实现，由它们来决定具体实现什么接口
 */
class InvokeFactory1 extends Factories {

    @Override
    public Product factoryMethod() {
        return new Product1();
    }
}

class InvokeFactory2 extends Factories {

    @Override
    public Product factoryMethod() {
        return new Product2();
    }
}
